package com.itheima.stock.controller;

import com.alibaba.excel.EasyExcel;
import com.itheima.stock.api.StockControllerAPI;
import com.itheima.stock.dto.StockInfoExcelDTO;
import com.itheima.stock.entity.StockBusiness;
import com.itheima.stock.mapper.StockMarketIndexInfoMapper;
import com.itheima.stock.properties.MarketIndexProperties;
import com.itheima.stock.response.PageResult;
import com.itheima.stock.response.ResponseResult;
import com.itheima.stock.service.StockService;
import com.itheima.stock.util.DateTimeUtil;
import com.itheima.stock.vo.*;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RestController;
import com.itheima.stock.constant.Constants;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
public class StockController implements StockControllerAPI {

    private final StockService stockService;

    public StockController(StockService stockService, StockMarketIndexInfoMapper stockMarketIndexInfoMapper, MarketIndexProperties marketIndexProperties) {
        this.stockService = stockService;
    }

/*
-----------------------------------------------------------------------------------------
 */

    @Override
    public ResponseResult<TradevolVO<OptionVo>> tradeAmt() {
       return ResponseResult.ok(stockService.tradeAmt());
    }

    @Override
    public ResponseResult<FileDownloadVO> export(int page, int pageSize, HttpServletResponse response) throws IOException {
        //设置相应数据类型 excel文件
        response.setContentType("application/vnd.ms-excel");
        //指定响应编码
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(Constants.EXCEL_FILE_PREFIX + page + Constants.EXCEL_FILE_Sufix, StandardCharsets.UTF_8);
        //指定文件名称（处理中文乱码问题）
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        //使用EasyExcel 实现文件导出
        //4.1 准备要写的数据 查询数据库
        AtomicInteger counter = new AtomicInteger(0);
        //将List集合数据写入到Excel中
        EasyExcel.write(response.getOutputStream(), StockInfoExcelDTO.class)
                .sheet(Constants.EXCEL_FILE_PREFIX+page+Constants.EXCEL_FILE_Sufix)
                .doWrite(stockService.more(page,pageSize).getRows().stream().map((vo)->{
                    StockInfoExcelDTO dto = new StockInfoExcelDTO();
                    BeanUtils.copyProperties(vo,dto);
                    dto.setNum(String.valueOf(counter.incrementAndGet()));
                    return dto;
                }).toList());
        return ResponseResult.ok(FileDownloadVO.builder().fileName(fileName).build());
    }

    @Override
    public ResponseResult<UpDownVO<OptionVo>> upDown() {
        return ResponseResult.ok(stockService.UpDown());
    }

    @Override
    public ResponseResult<PageResult<MoreVO>> more(Integer page, Integer PageSize) {
        return ResponseResult.ok(stockService.more(page,PageSize));
    }

    @Override
    public ResponseResult<List<IncreaseVO>> stockInCrease() {
        return ResponseResult.ok(stockService.selectStockInCrease());
    }

    @Override
    public ResponseResult<List<StockBlockInfoVO>> stockControllerApi() {
        return ResponseResult.ok(stockService.stockControllerApi());
    }

    @Override
    public ResponseResult<List<MarketIndexVO>> innerIndexAll() {
        return ResponseResult.ok(stockService.innerIndexAll());
    }



    @Override
    public ResponseResult<List<StockBusiness>> getStockBusiness() {
        return ResponseResult.ok(stockService.getStockBusiness());
    }
}
